//
//  ListTableViewController.m
//  Slice
//
//  Created by lanou on 16/6/16.
//  Copyright © 2016年 wangjinchao. All rights reserved.
//

#import "ListTableViewController.h"
#import "ReadDetailViewController.h"
@interface ListTableViewController ()

@end

@implementation ListTableViewController
/*
//添加下拉 和 上拉控件
- (void)getHeader {
//  创建头部控件提供了两种方法
//    1.直接使用block
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        //刷新数据前删除旧的数据
        [self.dataArr removeAllObjects];
//        重新请求新的数据
        [self getData];
//        结束刷新
        [self.tableView.mj_header endRefreshing];
    }];
////    2.使用target
//    MJRefreshNormalHeader *header1 = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(click:)];
//    [header1 endRefreshing];
//    
//    self.tableView.mj_header = header1;
 
    
//   使用动画的头部控件
    MJRefreshGifHeader *gif = [MJRefreshGifHeader headerWithRefreshingBlock:^{
        
    }];
//    装在动画视图
    NSMutableArray *marr = [NSMutableArray array];
    for (int i = 0; i  < 23; i++) {
        UIImage *img = [UIImage imageNamed:[NSString stringWithFormat:@"/Users/lanou/Desktop/shs100264/项目A段/项目分析、项目框架的搭建/Slice/比武/u=137554866,2899797031&fm=21&gp=0－%d（被拖移）.tiff",i]];
        [marr addObject:img];
    }
//    设置头部控件
    [gif setImages:marr duration:2 forState:    MJRefreshStateRefreshing ];
    
    self.tableView.mj_header = gif;
//    隐藏最后的更新lable
    gif.lastUpdatedTimeLabel.hidden = YES;
//   修改现实的文字
[gif setTitle:@"hahah" forState:MJRefreshStatePulling];

}

- (void)getFooter {
////    上拉控件
//    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
//        [self getData];
//        [self.tableView.mj_footer endRefreshing];
//    }];

//    上拉动画
    //   使用动画的头部控件
   MJRefreshAutoGifFooter *gif = [MJRefreshAutoGifFooter footerWithRefreshingBlock:^{
       
   }];
    //    装在动画视图
    NSMutableArray *marr = [NSMutableArray array];
    for (int i = 0; i  < 23; i++) {
        UIImage *img = [UIImage imageNamed:[NSString stringWithFormat:@"/Users/lanou/Desktop/shs100264/项目A段/项目分析、项目框架的搭建/Slice/比武/u=137554866,2899797031&fm=21&gp=0－%d（被拖移）.tiff",i]];
        [marr addObject:img];
    }
    //    设置头部控件
    [gif setImages:marr duration:2 forState:    MJRefreshStateRefreshing ];
    
    self.tableView.mj_footer = gif;

    //   修改现实的文字
    [gif setTitle:@"123123123" forState:MJRefreshStatePulling];

}

*/

- (void)getHeaderAndFooter {
    //  下拉加载
    self.tableView.mj_header  = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            [self.dataArr removeAllObjects];
            [self getData];
    }];
    
    
    // 上拉刷新
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
                    [self getData];
    }];
    

}



- (NSMutableArray *)dataArr {
    if (_dataArr == nil) {
        _dataArr = [NSMutableArray array];
    }
    return _dataArr;
}


- (void)getData {
    NSDictionary *dic = @{@"deviceid":@"63A94D37-33F9-40FF-9EBB-481182338873",@"typeid":self.typeId,@"client":@"1",@"sort":@"addtime",@"limit":@10,@"version":@"3.0.2",@"auth":@"Wc06FCrkoq1DCMVzGMTikDJxQ8bm3Mrm2NpT9qWjwzcWP23tBKQx1c4P0",@"start":@(self.dataArr.count)};
    [RequestTool requsetWithType: POST URLString:kReadLisURL paremeter:dic callBack:^(NSData *data, NSError *error) {
        if (!error) {
            NSDictionary *dataDic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves) error:nil];
            NSDictionary *dic1 = dataDic[@"data"];
            NSArray *dataArr = dic1[@"list"];
           
            for (NSDictionary *dic2  in dataArr ) {

                [self.dataArr addObject:[ListMdel modelWithDiction:dic2]];
                dispatch_async(dispatch_get_main_queue(), ^{
                   

                    [self.tableView reloadData];

                    [self.tableView.mj_header endRefreshing];
                    [self.tableView.mj_footer endRefreshing];
                });
            }
         } else {
            NSLog(@"%@",error.description);
        }
    }];
}







- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self getData];
    
    [self getHeaderAndFooter];
    
//    self.navigationItem.title = 
  
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    
    [self.tableView registerNib:[UINib nibWithNibName:@"ListTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"123"];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 180;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"123" forIndexPath:indexPath];
    
    [cell getDataWith:self.dataArr[indexPath.row]];
    
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//     取出点击的id
    ListMdel *model = self.dataArr[indexPath.row];
    
    ReadDetailViewController *detailVC  = [[ReadDetailViewController alloc] init];
    detailVC.contentid = model.id;
    detailVC.model = self.dataArr[indexPath.row];
    
    [self.navigationController pushViewController:detailVC animated:YES];
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
